package com.java.controller;

import com.java.pojo.*;
import com.java.pojo.EnterGood;
import com.java.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/Maker1")
public class MakerController {
    @Autowired
    private Maker1Service maker1Service;
    @Autowired
    private Maker2Service maker2Service;
    @Autowired
    private GoodStyleService goodStyleService;
    @Autowired
    private RepositoryService repositoryService;



    @RequestMapping(value = "/findAllByPage",produces = "text/html;charset=UTF-8")
    public String findAllByPage2(@RequestParam(value = "start",required = false) Integer start, @RequestParam(value = "forname",required = false)String forname , Model model){
        if(forname==null ||"".equals(forname)){
            forname="";
        }
        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+forname);
        /*计算总页数*/
        int totalCount = maker1Service.getCount(forname);
        int i = totalCount % 4 ==0? totalCount/4 :(totalCount/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }
        Page<Maker1> page =maker1Service.getPageByPage(start,forname);
        System.out.println("page:"+page);
        model.addAttribute("page",page);
        model.addAttribute("forname",forname);
        return "maker1/findAllByPage";
    }

    /*添加页面*/
    @RequestMapping("/addMaker1")
    public String addMaker1(Model model){
        List<GoodStyle> goodStyleList = goodStyleService.getGoodStyleList();
        List<Repository> reList = repositoryService.getReList();
        model.addAttribute("goodStyleList",goodStyleList);
        model.addAttribute("reList",reList);
        return "maker1/addMaker1";
    }


    /*添加*/
    @RequestMapping(value = "/addMakerOK",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addMakerOK(
            @RequestParam(value = "forname",required = false) String forname,
            @RequestParam(value = "goodstyle",required = false) Integer goodstyle,
            @RequestParam(value = "data1",required = false) String data1,
            @RequestParam(value = "rid",required = false) Integer rid,
            @RequestParam(value = "whe",required = false) String whe
    ) throws ParseException {
        Maker1 maker1=new Maker1();


        /*编号*/
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf2.format(new Date());
        String result="";
        String s="XSDD";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        System.out.println(s+newDate+result);
        String document=s+newDate+result;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date2 = sdf.parse(data1); //日期

        maker1.setDocument(document);//编号
        maker1.setData1(date2);//时间
        maker1.setForname(forname);//客户
        maker1.setRid(rid);//仓库
        maker1.setGoodstyle(goodstyle);//类型
        maker1.setWhe(whe);//地址
        /*添加操作*/
        int i = maker1Service.addMaker1(maker1);
        if(i>0){
            return "<script>alert('添加成功欢迎回来');location.href='/Maker1/findAllByPage';</script>";
        }
        else {
            return "<script>alert('添加失败!');location.href='/Maker1/findAllByPage';</script>";
        }
    }


    //删除
    @RequestMapping(value = "/DELMAKER",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String DELMAKER(@RequestParam(value = "mid",required = false) Integer mid){
        int i = maker1Service.deleteMaker1(mid);
        if(i>0) {
            return "<script>alert('删除successful');location.href='/Maker1/findAllByPage';</script>";
        }else {
            return "<script>alert('删除失败!');location.href='/Maker1/findAllByPage';</script>";
        }
    }


    //销售
    @RequestMapping(value = "/XSMaker1",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String XSMaker1(@RequestParam("mid") Integer mid){
        Maker1 maker1ByMid = maker1Service.getMaker1ByMid(mid);//主键查询
        Integer okandno = maker1ByMid.getOkandno();
        if(okandno==0) {
            boolean b = maker1Service.MAK(mid);
            if (b) {
                return "<script>alert('销售successful');location.href='/Maker1/findAllByPage';</script>";
            } else {
                return "<script>alert('销售失败!');location.href='/Maker1/findAllByPage';</script>";
            }
        }else {
            return "<script>alert('该订单已被销售!');location.href='/Maker1/findAllByPage';</script>";
        }
    }

    //退货
    @RequestMapping(value = "/THMaker1",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String THMaker1(@RequestParam("mid") Integer mid){
        boolean b = maker1Service.THM(mid);
        if(b){
            return "<script>alert('退货successful');location.href='/Maker1/findAllByPage';</script>";
        }else {
            return "<script>alert('还未销售!');location.href='/Maker1/findAllByPage';</script>";
        }
    }
}
